﻿using INTEX.DataBase.Tools;
using System;
using System.Collections.Generic;
using System.Data;
namespace WPFExcelReport
{
    public partial class VYGRONAV95
    {

        public DataRow NewABA()
        {
            DataRow dwData = INTEXDB.ICC_ABAX.NewRow();
            foreach (DataColumn cm in INTEXDB.ICC_ABAX.Columns)
            {
                if (dwData[cm.ColumnName].ToString() == "")
                {
                    if (cm.DataType.ToString().ToLower().Contains("string"))
                    {
                        dwData[cm.ColumnName] = "".PadRight(cm.MaxLength);
                    }
                    else
                    {
                        dwData[cm.ColumnName] = 0;
                    }
                }
            }
            return dwData;
        }
        /// <summary>
        /// 1.自动生产生产单所需要的必要数据
        /// 2.利用ICC_ABAX表明生产单之间的计划关系
        /// 3.产生原料需求单
        /// </summary>
        /// <param name="dwInput"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        double R01 = 0, R02 = 0, F01 = 0, F02 = 0, JNUM = 0, JWASTGE = 0;
        double QTYEND = 0;
        double QTYMAT = 0;
        string DELDATE = "0";
        string STARTDATE = "0";
        Dictionary<string, int> ABK;
        public DataSet PrePlan(DataSet SetEnv, DataSet DB, DataRow dwVKPOS)
        {
            ABK = new Dictionary<string, int>();
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            double VKPQTY = 0;
            tbABAUFM = DB.Tables["ICC_ABAX"].Clone();
            DataRow dwMU;
            string krs = "", lfd = "", pos = "";
            krs = dwVKPOS["VKPAUFKRS"].ToString();
            lfd = dwVKPOS["VKPAUFLFD"].ToString();
            pos = dwVKPOS["VKPAUFPOS"].ToString();
            string filter = "";
            DataRow[] rows;
            #region======Requirement=======
            DataRow dwVKGAAUF = null;
            filter = "GAAUFKRS='" + krs + "' AND GAAUFLFD='"
               + lfd + "' AND GAAUFPOS='" + pos + "' AND GASA='610' ";
            rows = DB.Tables["GAAUF"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwVKGAAUF = row;
                break;
            } 
            
            #endregion
            #region==============PRULES=====================

            var qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                         where row["ARTSTAGE"].ToString().Trim() == "S"
                         select row;
            DataRow dwRule = null;
            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }

            QTYEND = 0; QTYMAT = 0; R01 = 0; F01 = 0;
            
            if (dwRule != null)
            {
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;
                R02 = Convert.ToDouble(dwRule["R02"].ToString()) / 100;
                F01 = Convert.ToDouble(dwRule["F01"].ToString());
                F02 = Convert.ToDouble(dwRule["F02"].ToString());
              
                DELDATE = dwRule["DELDATE"].ToString();//销售单交期
                STARTDATE = dwRule["STARTDATE"].ToString();//销售单交期

                QTYEND = Convert.ToDouble(dwRule["QTYEND"].ToString());
                QTYMAT = Convert.ToDouble(dwRule["QTYMAT"].ToString());
            }
            if (QTYEND < 0)//如果用户没有设定
            {
                QTYEND = Convert.ToDouble(dwVKGAAUF["GAKG1"].ToString()) / 100;
            }
            if (QTYMAT < 0)
            {
                QTYMAT = (QTYEND + F01 - F02) * (1 + R01 + R02);
            }
            //QTYMAT = (QTYEND + F01 - F02) * (1 + R01 + R02);
            //QTYMAT = QTYEND;
            if (DELDATE.Length < 8)
            {
                DELDATE = date;
            }
            VKPQTY = QTYEND;
            #endregion

            //根据需求创建对应生产单信息，并且表达计划对应关系            
            dwMU = tbABAUFM.NewRow();
            dwMU.ItemArray = NewABA().ItemArray;

            dwMU["ABAID"] = ABAID;
            dwMU["ABAMANDANT"] = "00";
            dwMU["ABAAUFTYP"] = "N";
            dwMU["ABAVKAUFTYP"] = "K";
            dwMU["ABAVKAUFKRS"] = dwVKGAAUF["GAAUFKRS"];
            dwMU["ABAVKAUFLFD"] = dwVKGAAUF["GAAUFLFD"];
            dwMU["ABAVKAUFPOS"] = dwVKGAAUF["GAAUFPOS"];
            dwMU["ABAVKUPOS"] = dwVKGAAUF["GAPOS"];
            if (dwMU["ABAVKUPOS"].ToString().Trim() == "00")
            {
                dwMU["ABAVKUPOS"] = "01";
            }
            dwMU["QTYENDDES"] = dwRule["F02DES"].ToString().Trim();
            dwMU["ABAVB"] = dwVKGAAUF["VKPVB"];
            dwMU["ABAVG"] = dwVKGAAUF["VKPVG"];
            dwMU["ABAVLVB"] = dwVKGAAUF["VKPVB"];
            dwMU["ABAVLVG"] = dwVKGAAUF["VKPVG"];
            dwMU["ABARVSACHB"] = dwVKGAAUF["VKPSACHBE"];
            dwMU["ABAVLSACHB"] = dwVKGAAUF["VKPSACHBE"];
            dwMU["ABARKUNDNR"] = dwVKGAAUF["VKPKUNDNR"];
            dwMU["ABAVLVB"] = dwVKGAAUF["VKPVB"];
            dwMU["ABAVLVG"] = dwVKGAAUF["VKPVG"];
            dwMU["REMARK"] = dwVKPOS["REMARK"];
            dwMU["F02DES"] = dwVKPOS["REPEATORDER"];
            dwMU["F02"] = dwRule["F02"];
            dwMU["UNITNUM"] = dwRule["UNITNUM"];
            dwMU["UNITQTY"] = dwRule["UNITQTY"];
            dwMU["ABACOLORREF"] = dwRule["ORDERNUM"];
            dwMU["R01"] = dwRule["R01"];
            dwMU["R02"] = dwRule["R02"];
            dwMU["F01"] = dwRule["F01"];
            dwMU["UNDES"] = dwRule["UNDES"];
            dwMU["QTYEND"] = QTYEND;
            dwMU["QTYMAT"] = QTYMAT;
            dwMU["DELDATE"] = DELDATE;
            dwMU["STARTDATE"] = DELDATE;
            dwMU["ABARESMG"] =Math.Ceiling(QTYMAT * 1000);
            dwMU["ABAAUFMSL"] = "01";// dwVKGAAUF["VKPAUFMART"];
            dwMU["ABAPACKTYP"] = "BZL ";

            dwMU["ABASSTKLG"] = dwVKGAAUF["VKPLAEVON"];
            dwMU["EXART"] = krs + "-" + Helpers.Helper.Right(lfd, 4) + "-" + Helpers.Helper.Right(pos, 3);
            dwMU["ABASANZSTK"] = QTYMAT;
            dwMU["ABASSTKGEW"] = 1000;

            dwMU["ABAWAKZ"] = "G";
            dwMU["ABAART"] = dwVKGAAUF["GAART"];
            dwMU["ABAVAR"] = dwVKGAAUF["GAVAR"];
            dwMU["ABAFARBE"] = dwVKGAAUF["GAFARBE"];

            dwMU["ABAVKENDD"] = DELDATE;
            dwMU["ABAVKSTARTD"] = STARTDATE;
            dwMU["ABAUSER"] = app.User;
            dwMU["ABADATE"] = date;
            tbABAUFM.Rows.Add(dwMU);
            ABAID++;
            dwRule = null;
            string KRS0 = "B";
            if (GSFACH > 1)
            {
                qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                         where row["ARTSTAGE"].ToString().Trim() == "T"
                         select row;

                foreach (DataRow row in qRules)
                {
                    dwRule = row;
                }
                KRS0 = Helpers.Helper.Left(dwRule["ART"].ToString(), 1);
                dwMU["ABAAUFKRS"] = KRS0 + year.Substring(3, 1);
                if (ABPLFD == "X")
                {
                    ABPLFD = INTEXBL.GetEXAUFLFD(KRS0, year.Substring(3, 1), GSGARNNR.ToString(), "ABKOPF");
                }
                dwMU["ABAAUFLFD"] = ABPLFD;
                dwMU["ABAAUFPOS"] = "030";
                SResult = PlanTAB50(SetEnv, DB, dwMU, dwRule, VKPQTY);
                SReturn.Merge(SResult);
            }

            filter = "ABAAUFTYP='N' AND ABAWAKZ='G'";
            rows = tbABAUFM.Copy().Select(filter);
            qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                     where row["ARTSTAGE"].ToString().Trim() == "X"
                     select row;

            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }
            int pox = 20;
            foreach (DataRow dwABA in rows)
            {
                if (GSFACH == 1)
                {
                    KRS0 = Helpers.Helper.Left(dwRule["ART"].ToString(), 1);
                    dwABA["ABAAUFKRS"] = KRS0 + year.Substring(3, 1);
                    if (ABPLFD == "X")
                    {
                        ABPLFD = INTEXBL.GetEXAUFLFD(KRS0, year.Substring(3, 1), GSGARNNR.ToString(), "ABKOPF");
                    }
                    dwABA["ABAAUFLFD"] = ABPLFD;                   
                }
                dwABA["ABAAUFPOS"] = Helpers.Helper.Right("000" + pox, 3);
                SResult = PlanXAB50(SetEnv, DB, dwABA, dwRule, VKPQTY);
                pox++;
                SReturn.Merge(SResult);
            }
            filter = "ABAAUFTYP='N' AND ABAWAKZ='G'";
            rows = tbABAUFM.Copy().Select(filter);
            qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                     where row["ARTSTAGE"].ToString().Trim() == "R"
                     select row;

            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }
            pox = 10;
            foreach (DataRow dwABA in rows)
            {
                dwABA["ABAAUFPOS"] = Helpers.Helper.Right("000" + pox, 3);
                SResult = PlanRAB50(SetEnv, DB, dwABA, dwRule, VKPQTY);
                SReturn.Merge(SResult);
                pox++;
            }
            foreach (DataRow row in tbABAUFM.Rows)
            {
                if (row["ABAAUFPOS"].ToString() == "")
                {
                    row["ABAAUFKRS"] = "00";
                    row["ABAAUFLFD"] = "000000";
                }
            }
            SReturn.Merge(tbABAUFM);
            return SReturn;
        } 
    }
}
